package com.tanhua.dubbo.api;

import cn.hutool.core.collection.CollUtil;
import com.tanhua.dubbo.api.UserLocationApi;
import com.tanhua.model.mongo.UserLocation;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.geo.Circle;
import org.springframework.data.geo.Distance;
import org.springframework.data.geo.Metrics;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.geo.GeoJsonPoint;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;

import java.util.List;

@DubboService
public class UserLocationApiImpl implements UserLocationApi {

    @Autowired
    private MongoTemplate mongoTemplate;

    // 更新或者保存地理位置
    public Boolean updateLocation(Long userId, Double longitude, Double latitude, String address) {
        try{
            // 1:先根据用户id去查询地理位置信息
            Query query = Query.query(Criteria.where("userId").is(userId));
            UserLocation location = mongoTemplate.findOne(query, UserLocation.class);
            // 2:如果没有数据,进行保存
            if (location == null){
                UserLocation userLocation = new UserLocation();
                userLocation.setUserId(userId);
                userLocation.setAddress(address);
                userLocation.setCreated(System.currentTimeMillis());
                userLocation.setUpdated(System.currentTimeMillis());
                location.setLastUpdated(System.currentTimeMillis());  // 上一次更新时间
                location.setLocation(new GeoJsonPoint(longitude,latitude)); // x,y 经纬度,顺序一定要写对
                mongoTemplate.save(location);
            }else{
                // 3:如果查询到数据,将数据进行更新
                Update update = Update.update("location",new GeoJsonPoint(longitude,latitude))  // 以地理坐标为字段进行更新
                        .set("update",System.currentTimeMillis())   // 设置更新时间
                        .set("lastUpdated", location.getUpdated()); // 设置上次更新时间
                mongoTemplate.updateFirst(query,update,UserLocation.class);
            }
            return true;
        }catch (Exception e){
            e.printStackTrace();
            return false;
        }
    }

    // 查询附近的人的用户id
    public List<Long> queryNearUser(Long userId, Double meter) {
        // 1:根据用户id查询用户的位置信息
        Query query = Query.query(Criteria.where("userId").is(userId));
        UserLocation location = mongoTemplate.findOne(query, UserLocation.class);
        if (location == null){
            return null;
        }
        // 2:以当前用户位置绘制原点
        GeoJsonPoint point = location.getLocation();
        // 3:绘制半径                       // 除1000相当于以 米 做单位 ,后面的公里
        Distance distance = new Distance(meter/1000, Metrics.KILOMETERS);
        // 4:绘制圆形
        Circle circle = new Circle(point, distance);
        // 5:进行查询
        Query query1 = Query.query(Criteria.where("location").withinSphere(circle));
        List<UserLocation> list = mongoTemplate.find(query1, UserLocation.class);
        return CollUtil.getFieldValues(list,"userId",Long.class);
    }
}
